<!DOCTYPE html>
<html>
<head>
	<!-- 公共页面 -->
	#include("../include/common.html")
	<!-- 公共页面 /-->
</head>
<body class="hold-transition skin-purple sidebar-mini">
    <div class="wrapper">
        <!-- 页面头部 -->
        #include("../include/header.html")
        <!-- 页面头部 /-->
        <!-- 导航侧栏 -->
        #include("../include/menu.html")
        <!-- 导航侧栏 /-->
        <!-- 内容区域 -->
        <div class="content-wrapper" id="contentDiv">
            <!-- 正文区域 -->
            <section class="content">
				<!-- .box-body -->
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">表单数据管理</h3>
                    </div>
                    <div class="box-body">
                    	<div class="col-md-2">
                    		<ul id="dataTree" class="ztree" style="height: 100%"></ul>
                    	</div>
                    	<div class="col-md-10" id="dataBody">
                    	</div>
                    </div>
                    <!-- /.box-body -->
                </div>
            </section>
            <!-- 正文区域 /-->
        </div>
        <!-- 内容区域 /-->
        <!-- 底部导航 -->
        #include("../include/footer.html")
        <!-- 底部导航 /-->
    </div>
</body>
</html>
<!---->
<script>
 $(document).ready(function() {
     // 激活导航位置
     setSidebarActive("admin-formData");
 });
</script>
<script type="text/javascript">
var dataFrame;
var setting = {
  view: {
    dblClickExpand: false,
    showLine: true,
    selectedMulti: false
  },
  data: {
    simpleData: {
      enable: true,
      idKey: "id",
      pIdKey: "pId",
      rootPId: ""
    }
  },
  callback: {
    beforeClick: function (treeId, treeNode) {
    	var formId=treeNode.id;
    	$("#formId").val(formId);
      	loadData({"formId":formId});
      return true;
    }
  }
};

var nodes = [
	#for(form : forms)
    {id:#(form.id),open:true,pId:0, name: "#(form.name)"}#if(!for.last),#end
	#end	
];

$(document).ready(function(){
    var treeObj = $.fn.zTree.init($("#dataTree"), setting, nodes);
    var treeNodes = treeObj.getNodes();
    if (treeNodes.length > 0) {
        treeObj.selectNode(treeNodes[0]);//默认选中根节点
        var formId=treeNodes[0].id;
        $("#formId").val(formId);
        loadData({"formId":formId});
    }
    dataFrame = $("#dataFrame");
});
</script>
<script type="text/javascript">
function loadData(param){
	$.ajax({
		url: "#(base)/admin/form_data/data",
		type: "POST",
		data: param,
		dataType: "html",
		cache: false,
		success: function(response) {
			var html=response;
           	$("#dataBody").html(html);
		}
	});
}

//将form数据转成json对象
function getFormData($form) {
    var serializeArray = $form.serializeArray();
    var json = {};

    $.map(serializeArray, function (n, i) {
    	json[n['name']] = n['value'];
    });

    return json;
}

//将search数据转成json对象
function getSearchData(){
	var listQuery = getCookie("listQuery");
	var json={};
	if(!isEmpty(listQuery)){
		if(listQuery.indexOf("?")!=-1){
			listQuery=listQuery.substring(listQuery.indexOf("?")+1);
		}
		var params=listQuery.split("&");
		for(var i=0;i<params.length;i++){
			json[params[i].split("=")[0]]=params[i].split("=")[1];
		}
	}
	return json;
}

//form表单返回
function formBack(){
	loadData(getSearchData());
}

//form表单提交
function formSubmit(form){
	var url = form.attr("action");
	var data = form.serialize();
	$.ajax({
        url: url,
        type: "POST",
        data: data,
        dataType: "json",
        cache: false,
        success: function(response) {
            if (response.type == "success") {
            	swal({title:'', text:'操作成功',icon: 'success',button:false,timer: 2000}).then(function(){
	            	loadData(getSearchData());
            	});
            }else{
                swal({title:'', text:response.msg,icon: 'error'});
            }
        }
    });
}

//添加
function add(formId){
	$.ajax({
        url: "#(base)/admin/form_data/add?formId="+formId,
        type: "POST",
        data: {},
        dataType: "html",
        cache: false,
        success: function(response) {
           	var html=response;
           	$("#dataBody").html(html);
        }
    });
}

//修改
function edit(id,formId){
	$.ajax({
        url: "#(base)/admin/form_data/edit?id="+id+"&formId="+formId,
        type: "POST",
        data: {},
        dataType: "html",
        cache: false,
        success: function(response) {
           	var html=response;
           	$("#dataBody").html(html);
        }
    });
}

$().ready(function() {
	
	//新建
	$("#dataBody").on("click","#addButton",function(){
		var formId = $(this).attr("formId");
		add(formId);
	});
	
	//修改
	$("#dataBody").on("click",".editButton",function(){
		var formDataId = $(this).attr("formDataId");
		var formId = $(this).attr("formId");
		edit(formDataId,formId);
	});

});
</script>